#include <iostream>
#include <algorithm>
using namespace std;

const int N = 100050;
int n, m, idx;
int p[N];
struct Edge {
	int a, b, w;
	bool operator < (const Edge &b) const {
		return w < b.w;
	}
} e[N*2];

int find(int x){
	if (p[x] != x){ p[x] = find(p[x]);}
	return p[x];
}

int main(){
	cin >> n >> m;
	for (int i = 0; i <= n; i++){
		p[i] = i;
	}
	while (m--) {
		int a, b, w;
		cin >> a >> b >> w;
		e[idx++] = {a, b, w};
	}
	sort(e, e + idx);
	long long res = 0;
	int cnt = 0;
	for (int i = 0; i < idx; i++){
		int a = e[i].a, b = e[i].b, w = e[i].w;
		if (find(a) != find(b)){
			p[find(a)] = find(b);
			res += w;
			cnt++;
		}
	}
	if (cnt == n - 1){
		cout << res << endl;
	}else {
		cout << "impossible" << endl;
	}
	return 0;
}
